package gui;

import ge.escola.Laboratorio;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SelecionaLaboratorio extends javax.swing.JPanel {
	private JLabel title;
	private JButton selecionar;
	private JButton jButton1;
	private JLabel caixaDoMeio;
	private JLabel caixaDosBotoes;
	private JLabel nomeLab;
	private JTextField caixaDeProcura;
	private JButton voltar;
	private JTextArea listaDeLabs;
	public static Laboratorio selecionado;

	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new SelecionaLaboratorio());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public SelecionaLaboratorio() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(800, 600));
			this.setBackground(new java.awt.Color(209, 209, 255));
			this.setLayout(null);
			{
				title = new JLabel();
				this.add(title);
				title.setText("                   Selecione o Laboratorio");
				title.setBounds(19, 21, 763, 59);
				title.setFont(new java.awt.Font("Arial", 0, 36));
				title.setForeground(new java.awt.Color(64, 128, 128));
				title.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				listaDeLabs = new JTextArea();
				this.add(listaDeLabs);
				listaDeLabs.setText("Nao existem laboratorios cadastrados");
				listaDeLabs.setBounds(19, 103, 421, 466);
				listaDeLabs.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				listaDeLabs.setFont(new java.awt.Font("Arial", 0, 14));
				listaDeLabs.setEditable(false);
				listaDeLabs.setBackground(new java.awt.Color(255, 255, 255));
				listaDeLabs.setText(carregaLabs());
			}
			{
				selecionar = new JButton();
				this.add(selecionar);
				selecionar.setText("Selecionar");
				selecionar.setBounds(627, 300, 100, 23);
				selecionar.setFont(new java.awt.Font("Arial", 0, 12));
				selecionar.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				selecionar.setBackground(new java.awt.Color(255, 255, 255));
				selecionar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(java.awt.event.MouseEvent evt) {
						buttonSelecionarClicked(evt);
					}
				});
			}
			{
				voltar = new JButton();
				this.add(voltar);
				voltar.setText("Voltar");
				voltar.setBounds(627, 465, 100, 23);
				voltar.setFont(new java.awt.Font("Arial", 0, 12));
				voltar.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				voltar.setBackground(new java.awt.Color(255, 255, 255));
				voltar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(java.awt.event.MouseEvent evt) {
						buttonVoltarClicked(evt);
					}
				});
			}
			{
				jButton1 = new JButton();
				this.add(jButton1);
				jButton1.setText("Excluir");
				jButton1.setBackground(new java.awt.Color(255, 255, 255));
				jButton1.setFont(new java.awt.Font("Arial", 0, 12));
				jButton1.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				jButton1.setBounds(627, 384, 100, 23);
				jButton1.addMouseListener(new MouseAdapter() {
					public void mouseClicked(java.awt.event.MouseEvent evt) {
						buttonDeleteClicked(evt);
					}
				});
			}
			{
				caixaDeProcura = new JTextField();
				this.add(caixaDeProcura);
				caixaDeProcura.setBounds(500, 191, 288, 23);
				caixaDeProcura.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				caixaDoMeio = new JLabel();
				this.add(caixaDoMeio);
				caixaDoMeio.setBounds(458, 103, 24, 466);
				caixaDoMeio.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				nomeLab = new JLabel();
				this.add(nomeLab);
				nomeLab.setText("Digite o nome do laboratorio");
				nomeLab.setBounds(500, 159, 282, 21);
				nomeLab.setFont(new java.awt.Font("Arial", 0, 18));
				nomeLab.setForeground(new java.awt.Color(64, 128, 128));
				nomeLab.setHorizontalAlignment(SwingConstants.CENTER);
				nomeLab.setHorizontalTextPosition(SwingConstants.CENTER);
			}
			{
				caixaDosBotoes = new JLabel();
				this.add(caixaDosBotoes);
				caixaDosBotoes.setBounds(590, 265, 182, 261);
				caixaDosBotoes.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void buttonVoltarClicked(java.awt.event.MouseEvent evt) {
		GerenciadorDeJanelas.window
				.setContentPane(GerenciadorDeJanelas.menuAltera);
		GerenciadorDeJanelas.window.setVisible(true);
	}

	public void buttonDeleteClicked(java.awt.event.MouseEvent evt) {
		String nome = this.caixaDeProcura.getText().trim();
		for (Laboratorio lab : GerenciadorDeJanelas.todosLabs) {

			if (lab.getNomeDoLaboratorio().equalsIgnoreCase(nome)) {

				GerenciadorDeJanelas.todosLabs.remove(lab);
				String xmlLab = GerenciadorDeJanelas.ARQUIVADOR
						.toXML(GerenciadorDeJanelas.todosLabs);
				GerenciadorDeJanelas.gravar(
						GerenciadorDeJanelas.arquivoDeLaboratorios, xmlLab);
				listaDeLabs.setText(carregaLabs());
				break;
			} else {
				JOptionPane.showMessageDialog(null,
						"Laboratorio nao encontrado!", "Nome invalido!",
						JOptionPane.ERROR_MESSAGE);
				break;
			}
		}
	}

	public void buttonSelecionarClicked(java.awt.event.MouseEvent evt){
		String nome = this.caixaDeProcura.getText().trim();
		for (Laboratorio lab : GerenciadorDeJanelas.todosLabs) {
			if (lab.getNomeDoLaboratorio().equals(nome)) {
				selecionado = lab;
				GerenciadorDeJanelas.alteraLaboratorio = new AlteraLaboratorio();
				GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.alteraLaboratorio);
				GerenciadorDeJanelas.window.setVisible(true);
				break;
			} else {
				JOptionPane.showMessageDialog(null,
						"Laboratorio nao encontrado!", "Nome invalido!",
						JOptionPane.ERROR_MESSAGE);
				break;
			}
		}

		
	}
	private static String carregaLabs() {
		String listaLab = "";
		for (Laboratorio lab : GerenciadorDeJanelas.todosLabs) {
			listaLab += lab.toString()
					+ "\n\n---------------------------------------------------------\n";
		}
		return listaLab;
	}
}
